#include "md5.h"

#include <openssl/md5.h>
#include <stdio.h>

int md5sum(char *file, char *hash)
{
  FILE *f = fopen(file, "rb");
  if(!f)
    return 2;

  const int BUFFER_SIZE = 64;
  char buffer[BUFFER_SIZE];
  unsigned char digest[MD5_DIGEST_LENGTH];
  MD5_CTX ctx;

  MD5_Init(&ctx);

  int count;
  while(count = fread(buffer, sizeof(char), BUFFER_SIZE, f))
    MD5_Update(&ctx, buffer, count);

  fclose(f);

  MD5_Final(digest, &ctx);

  hash[32] = '\n';
  for(int i=0; i<16; i++)
    sprintf(hash+i*2, "%02x", digest[i]);

  return 1;
}
